<template>
  <!--财务管理-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-button type="primary" icon="el-icon-download" style="margin-right: 20px!important;" @click="handleExport">
              导出
            </el-button>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">公司名称:</div>
          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              style="width: 180px"
              placeholder="请输入公司名称"
              clearable
            />
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 120px">登录平台手机号:</div>
          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model.trim="queryForm.mobile"
              style="width: 180px"
              placeholder="请输入登录平台手机号"
              clearable
            />
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">交易类型：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.type"
              clearable
              placeholder="请选择交易类型"
              style="width: 180px"
            >
              <el-option
                v-for="item in type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">收支类型：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.pays"
              clearable
              placeholder="请选择收支类型"
              style="width: 180px"
            >
              <el-option
                v-for="item in pays"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 60px">状态：</div>
          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              clearable
              placeholder="请选择状态"
              style="width: 180px"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item>
            <div style="width: 80px">付款日期：</div>
          </el-form-item>-->
          <el-form-item>
            <el-date-picker
              v-model="queryForm.date"
              type="daterange"
              value-format="yyyy-M-d"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:250px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="restart"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
<!--      <vab-query-form-left-panel :span="2">
      </vab-query-form-left-panel>-->
    </vab-query-form>

    <el-table
      id="out-table"
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="company_name"
        label="公司名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="login_phone_number"
        label="登录平台手机号"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="trade_type_text"
        label="交易类型"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="payment_type_text"
        label="收支类型"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="transaction_amount"
        label="上账金额"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="total_balance"
        label="账户余额"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="deal_status_text"
        label="状态"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="transaction_time"
        label="上账时间"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="detail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 查看 -->
    <el-dialog title="详情" :visible.sync="dialogDetail">
      <div class="detail-cont" :model="details">
        <div class="list">
          公司名称:
          <span>{{ details.company_name }}</span>
        </div>
        <div class="list">
          登录平台手机号:
          <span>{{ details.login_phone_number }}</span>
        </div>
        <div class="list">
          交易类型:
          <span>{{ details.trade_type_text }}</span>
        </div>
        <div class="list">
          收支类型:
          <span>{{ details.payment_type_text }}</span>
        </div>
        <div class="list">
          上账金额:
          <span>{{ details.transaction_amount }}</span>
        </div>
        <div class="list">
          账户余额:
          <span>{{ details.total_balance }}</span>
        </div>
        <div class="list">
          状态:
          <span>{{ details.deal_status_text }}</span>
        </div>
        <div class="list">
          上账时间:
          <span>{{ details.transaction_time }}</span>
        </div>
        <div class="list">
          备注:
          <span>{{ details.notes }}</span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getRechargeRecordLists } from '@/api/common'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    name: 'Statistics',
    data() {
      return {
        type: [
          {
            value: 0,
            label: '请选择',
          },
          {
            value: 1,
            label: '充值',
          },
          {
            value: 2,
            label: '付款',
          },
          {
            value: 3,
            label: '退款',
          },
          {
            value: 4,
            label: '调账',
          },
        ],
        pays: [
          {
            value: 0,
            label: '请选择',
          },
          {
            value: 1,
            label: '收入',
          },
          {
            value: 2,
            label: '支出',
          },
        ],
        status: [
          {
            value: 0,
            label: '请选择',
          },
          {
            value: 1,
            label: '处理中',
          },
          {
            value: 2,
            label: '已完成',
          },
        ],
        queryForm: {
          account: '',
          mobile: '',
          name: '',
          type: '',
          pays: '',
          status: '',
          date: '',
        },
        form: {
          content: '',
        },
        disabled: false,
        listLoading: false,
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        dialogDetail: false,
        details: '',
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.queryForm.date = ''
        this.getData()
      },
      //导出
      handleExport() {
        /* 从表生成工作簿对象 */
        var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
        /* 获取二进制字符串作为输出 */
        var wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array',
        })
        try {
          FileSaver.saveAs(
            //Blob 对象表示一个不可变、原始数据的类文件对象。
            //Blob 表示的不一定是JavaScript原生格式的数据。
            //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
            //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
            new Blob([wbout], { type: 'application/octet-stream' }),
            //设置导出文件名称
            'outsheet.xlsx',
          )
        } catch (e) {
          if (typeof console !== 'undefined') console.log(e, wbout)
        }
        return wbout
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          transaction_time_begin: this.queryForm.date[0],
          transaction_time_end: this.queryForm.date[1],
          login_phone_number: this.queryForm.mobile,
          trade_type: this.queryForm.type,
          payment_type: this.queryForm.pays,
          deal_status: this.queryForm.status,
          company_name: this.queryForm.name,
        }
        getRechargeRecordLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      detail(row) {
        this.dialogDetail = true
        this.details = row
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
    margin-right: 20px;
  }
  .detail-cont {
    .list {
      margin-bottom: 20px;
      span {
        margin-left: 20px;
      }
    }
  }
</style>
